<template>
  <div class="reflect_page">
    <van-nav-bar :title="title" left-arrow @click-left="$router.back()" left-text="返回" />
    <van-form @submit="onSubmit">
      <van-field autocomplete="off"
        type="number"
        v-model="money"
        name="money"
        :label="id === 'obtainPay' ? '借支总金额' : '提现金额'"
        :placeholder="id === 'obtainPay' ? '请输入借支总金额' : '请输入提现金额'"
        required
        :rules="[
          {required: true, message: '请输入提现金额'},
          {message: '只能输入2位小数的正数', pattern}
        ]"
      />
      <van-field autocomplete="off"
        v-model="account"
        v-if="id !== 'obtainPay'"
        name="account"
        :label="cashWay[intoWay] ? cashWay[intoWay] + '号' : '提现账号'"
        placeholder="请输入提现账号"
        required
        :rules="[{required: true, message: '请输入提现账号'}]"
      />
      <van-field autocomplete="off"
        v-if="id !== 'obtainPay'"
        v-model="name"
        name="name"
        label="收款人姓名"
        placeholder="请输入收款人姓名"
        required
        :rules="[{required: true, message: '请输入收款人姓名'}]"
      />
      <van-field autocomplete="off"
        v-model="payPwd"
        type="password"
        name="payPwd"
        label="支付密码"
        placeholder="请输入支付密码"
        required
        :rules="[{required: true, message: '支付密码必须输入！'}]"
      />

      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">
          {{ this.$route.query.text || '确定提现' }}
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {Toast} from 'vant'

const cashWay = {
  card: '银行卡',
  wechat: '微信',
  qq: 'QQ',
  alipay: '支付宝'
}

export default {
  name: 'reflect',
  data() {
    return {
      payParams: {},
      cashWay,
      title: '提现',
      money: '',
      intoWay: '',
      name: '',
      account: '',
      payPwd: '',
      pattern: /^[0-9]+(\.[0-9]{0,2})?$/,
      cashOutSign: '',
      id: this.$route.params.id
    }
  },

  created() {
    if (this.$route.params.id) {
      if (this.$route.params.id === 'obtainPay') {
        this.title = '借支支付'
      }
      this.cashOutSign = this.$route.params.id
    } else {
      this.$router.back()
    }
  },

  mounted() {
    this.money = this.$route.query.total
    this.title = this.$route.query.title
    this.payParams = JSON.parse(this.$route.query.payParams)
  },
  methods: {
    onSubmit(values) {
      if (this.$route.query.from === 'upAndDownstreamCompany') {
        this.$api.job
          .cooperationPay({
            ...this.payParams,
            paypassword: this.payPwd
          })
          .then(res => {
            if (res.code === 200) {
              Toast(res.data)
              this.$router.back()
            } else {
              Toast(res)
            }
          })
      } else {
        const params = {
          account: values.account,
          cashname: values.name,
          money: values.money * 1000,
          paypassword: this.$Base64.encode(this.$md5(values.payPwd) + this.$md5('jinpin888')),
          acctype: values.intoWay
        }

        // 局部变量
        let url
        switch (this.cashOutSign) {
          case 'recommendPersons':
            url = ''
            this._cashOutSign_all(url, params)
            break
          case 'lendMoney':
            url = ''
            this._cashOutSign_all(url, params)
            break
          case 'wage':
            url = ''
            this._cashOutSign_all(url, params)
            break
          default:
            url = '/api/stock/cash'
            this._cashOutSign_all(url, params)
        }
      }
    },

    async _cashOutSign_all(url, params) {
      if (url) {
        const res = await this.$api.stock.reqDrawCash(url, params)
        if (res.code === 200) {
          Toast(res.data)
          await this.$router.back()
        }
      } else {
        Toast('提现参数有误，请退出登录重试！')
      }
    }
  }
}
</script>

<style scoped></style>
